﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Tssipl.PaymentAdapter.Core;
using Tssipl.PaymentAdapter.MessageFormat.Sfms;
namespace Tssipl.PaymentAdapter.MessageEntity.Sfms.Rtgs
{
    public class RtgsInterbankPaymentRequestOrNotification : SfmsMessageEntityBase, IMessageEntity
    {

        public SfmsBlock4 block4 { get; set; }

        public string TRN { get; set; }//16x //M
        public string RelatedReference { get; set; }        //16x //M

        private string _ValueDate_Currency_Amount { get; set; }//8!n3!a19d  //M
        public DateTime ValueDate { get; set; }
        public string Currency { get; set; }
        public decimal Amount { get; set; }

        public string OrderingInstitution1 { get; set; }//4!a1!n6!a 
        public string OrderingInstitution2 { get; set; }//[/1!a][/34x] 4*35x 

        public string SenderCorrespondent1 { get; set; }//4!a1!n6! a 
        public string SenderCorrespondent2 { get; set; }//[/1!a][/34x] [35x]
        public string SenderCorrespondent3 { get; set; }//[/1!a][/34x] [35x]

        public string ReceiverCorrespondent1 { get; set; }//4!a1!n6! a 
        public string ReceiverCorrespondent2 { get; set; }//[/1!a][/34x] [35x] 
        public string ReceiverCorrespondent3 { get; set; }//[/1!a][/34x] 4*35x 
        public string Intermediary1 { get; set; }//4!a1!n6! a 
        public string Intermediary2 { get; set; }//[/1!a][/34x] 4*35x 

        public string AccountWithInstitution1 { get; set; }//4!a1!n6! a 
        public string AccountWithInstitution2 { get; set; }//[/1!a][/34x] [35x] 
        public string AccountWithInstitution3 { get; set; }//[/1!a][/34x] 4*35x 

        public string BeneficiaryInstitution1 { get; set; }//4!a1!n6! a //M
        public string BeneficiaryInstitution2 { get; set; }//[/1!a][/34x] 4*35x //M
        public string SenderToReceiverInformation { get; set; }//6*35x

        public RtgsInterbankPaymentRequestOrNotification()
            : base()
        {

        }

        public RtgsInterbankPaymentRequestOrNotification(MessageParseResults results)
            : base(results)
        {
            SfmsMessage message = (SfmsMessage)results.getMessage();
            block4 = message.getBlock4();
            this.TRN = block4.getField("2020").Trim();
            this.RelatedReference = block4.getField("2006");
            this._ValueDate_Currency_Amount = block4.getField("4488");
            this.ValueDate = DateTime.ParseExact(this._ValueDate_Currency_Amount.Substring(0, 8), "yyyyMMdd", null);
            this.Currency = this._ValueDate_Currency_Amount.Substring(8, 3);
            this.Amount = decimal.Parse(this._ValueDate_Currency_Amount.Substring(11, this._ValueDate_Currency_Amount.Length - 11).Replace(",", "."));
            this.OrderingInstitution1 = block4.getField("5517");
            this.OrderingInstitution2 = block4.getField("5516");

            this.SenderCorrespondent1 = block4.getField("5518");
            this.SenderCorrespondent2 = block4.getField("6717");
            this.SenderCorrespondent3 = block4.getField("5521");

            this.ReceiverCorrespondent1 = block4.getField("6500");
            this.ReceiverCorrespondent2 = block4.getField("6718");
            this.ReceiverCorrespondent3 = block4.getField("5526");

            this.Intermediary1 = block4.getField("6511");
            this.Intermediary2 = block4.getField("5546");

            this.AccountWithInstitution1 = block4.getField("6516");
            this.AccountWithInstitution2 = block4.getField("6719");
            this.AccountWithInstitution3 = block4.getField("5551");

            this.BeneficiaryInstitution1 = block4.getField("6521");
            this.BeneficiaryInstitution2 = block4.getField("5556");

            this.SenderToReceiverInformation = block4.getField("7495");
        }

        new public string Build()
        {
            block4 = new SfmsBlock4();
            block4.Fields.Add(new Field("2020", this.TRN));
            block4.Fields.Add(new Field("2006", this.RelatedReference));
            block4.Fields.Add(new Field("4488", this.ValueDate.ToString("yyyyMMdd") + this.Currency.PadRight(3) + this.Amount.ToString().Replace(".", ",")));

            //this.Amount = decimal.Parse(this._ValueDate_Currency_Amount.Substring(11, this._ValueDate_Currency_Amount.Length - 11).Replace(",", "."));
            if (string.IsNullOrEmpty(this.OrderingInstitution1) == false) block4.Fields.Add(new Field("5517", this.OrderingInstitution1));
            //this.OrderingInstitution2 = block4.getField("5516");
            if (string.IsNullOrEmpty(this.OrderingInstitution2) == false) block4.Fields.Add(new Field("5516", this.OrderingInstitution2));
            //this.SenderCorrespondent1 = block4.getField("5518");
            if (string.IsNullOrEmpty(this.SenderCorrespondent1) == false) block4.Fields.Add(new Field("5518", this.SenderCorrespondent1));
            //this.SenderCorrespondent2 = block4.getField("6717");
            if (string.IsNullOrEmpty(this.SenderCorrespondent2) == false) block4.Fields.Add(new Field("6717", this.SenderCorrespondent2));
            //this.SenderCorrespondent3 = block4.getField("5521");
            if (string.IsNullOrEmpty(this.SenderCorrespondent3) == false) block4.Fields.Add(new Field("5521", this.SenderCorrespondent3));

            //this.ReceiverCorrespondent1 = block4.getField("6500");
            if (string.IsNullOrEmpty(this.ReceiverCorrespondent1) == false) block4.Fields.Add(new Field("6500", this.ReceiverCorrespondent1));
            //this.ReceiverCorrespondent2 = block4.getField("6718");
            if (string.IsNullOrEmpty(this.ReceiverCorrespondent2) == false) block4.Fields.Add(new Field("6718", this.ReceiverCorrespondent2));
            //this.ReceiverCorrespondent3 = block4.getField("5526");
            if (string.IsNullOrEmpty(this.ReceiverCorrespondent3) == false) block4.Fields.Add(new Field("5526", this.ReceiverCorrespondent3));

            //this.Intermediary1 = block4.getField("6511");
            if (string.IsNullOrEmpty(this.Intermediary1) == false) block4.Fields.Add(new Field("6511", this.Intermediary1));
            //this.Intermediary2 = block4.getField("5546");
            if (string.IsNullOrEmpty(this.Intermediary2) == false) block4.Fields.Add(new Field("5546", this.Intermediary2));

            //this.AccountWithInstitution1 = block4.getField("6516");
            if (string.IsNullOrEmpty(this.AccountWithInstitution1) == false) block4.Fields.Add(new Field("6516", this.AccountWithInstitution1));
            //this.AccountWithInstitution2 = block4.getField("6719");
            if (string.IsNullOrEmpty(this.AccountWithInstitution2) == false) block4.Fields.Add(new Field("6719", this.AccountWithInstitution2));
            //this.AccountWithInstitution3 = block4.getField("5551");
            if (string.IsNullOrEmpty(this.AccountWithInstitution3) == false) block4.Fields.Add(new Field("5551", this.AccountWithInstitution3));

            //this.BeneficiaryInstitution1 = block4.getField("6521");
            if (string.IsNullOrEmpty(this.BeneficiaryInstitution1) == false) block4.Fields.Add(new Field("6521", this.BeneficiaryInstitution1));
            //this.BeneficiaryInstitution2 = block4.getField("5556");
            if (string.IsNullOrEmpty(this.BeneficiaryInstitution2) == false) block4.Fields.Add(new Field("5556", this.BeneficiaryInstitution2));

            //this.SenderToReceiverInformation = block4.getField("7495");
            if (string.IsNullOrEmpty(this.SenderToReceiverInformation) == false) block4.Fields.Add(new Field("7495", this.SenderToReceiverInformation));

            return base.Build() + block4.getFormattedBlock();
        }
    }
}
